<!-- <template>
	<view>
		websocket
	</view>
</template>

<script>
	import {post} from '../../utils/request001.js'
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			
		},
		onLoad() {
			const res =  post('/api/auth/jwt/token', {
				username: 19571310176,
				password: 'wlm0403',
				type: 1,
				fingerprint: "就业星",
			})
			console.log(res);
			if (res.status === 200) {
				uni.setStorageSync('token', res.data)
				uni.navigateBack(-1)
			} else {
				this.showModal = true;
				this.modalContent = res.message;
			}
		}
	}
</script>

<style>

</style -->>

<template>
  <view>
    WebSocket Heartbeat Example
  </view>
</template>

<script>
// import { post } from '../../utils/request001.js'
export default {
  data() {
    return {
      socketTask: null,
    };
  },
  methods: {
    // 初始化WebSocket连接
    initWebSocket() {
      this.socketTask = uni.connectSocket({
        url: 'ws://localhost:8080/',
        success: () => {
          console.log('WebSocket连接成功');
          this.startHeartbeat();
        },
        fail: (error) => {
          console.error('WebSocket连接失败', error);
        }
      });
      
      this.socketTask.onOpen((res) => {
        console.log('WebSocket 已打开', res);
      });

      this.socketTask.onMessage((res) => {
        console.log('收到服务器内容：' + res.data);
      });

      this.socketTask.onError((error) => {
        console.error('WebSocket 错误', error);
      });

      this.socketTask.onClose((res) => {
        console.log('WebSocket 已关闭', res);
      });
    },
    
    // 发送心跳
    sendHeartbeat() {
      if (this.socketTask && this.socketTask.readyState === 1) {
        this.socketTask.send({ data: JSON.stringify({ type: '数据' }) });
      }
    },
    
    // 开始心跳
    startHeartbeat() {
      setInterval(() => {
        this.sendHeartbeat();
      }, 5000); // 每5秒发送一次心跳
    },
  },
  onLoad() {
    this.initWebSocket();
  },
  onUnload() {
    if (this.socketTask) {
      this.socketTask.close();
    }
  },
}

</script>

<style>
/* 样式根据您的需求添加 */
</style>
